package Card1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-03-03
 * Time: 21:08
 */
public class Card_Operation {
    public static final String[] suits = {"♥","♠","♣","♦"};

    public List<Card> generate_card() {
        List<Card> cardList = new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < 4; j++) {
                int num = i;
                String suit = suits[j];
                Card card = new Card(suit,num);
                cardList.add(card);
            }
        }
        return cardList;
    }

    public void shuffle(List<Card> cardList) {
        Random random = new Random();
        for (int i = cardList.size()-1; i > 0; i--) {
            int index = random.nextInt(i);
            swap(cardList,i,index);
        }
    }

    private void swap(List<Card> cardList, int i, int j) {
        //数组中的交换
/*        Card tmp = cardList[i];
        cardList[i] = cardList[j];
        cardList[j] = tmp;*/

        Card tmp = cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

    public List<List<Card>> play(List<Card> cardList) {
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        //发 3 次牌
        for (int i = 0; i < 3; i++) {
            //一次发牌，一个人直接获得 5 张
            for (int j = 0; j < 5; j++) {
                Card card = cardList.remove(0);
                hand.get(i).add(card);
            }
        }
        return hand;
    }
}
